<?php


/**
 * @author Admin
 *
 *
 */
class PageNavigator extends HtmlDisplay {
	/**
	 * 
	 * the current number 
	 * @var unknown_type
	 */
	public $currentNum=0;
	/**
	 * 
	 * the limit to display
	 * @var unknown_type
	 */
	public $limit=0;
	/**
	 * the total number in the collection
	 */
	public $collectionTotal;
	/**
	 *
	 */
	protected $id;

	public function __construct( $id='nav',$class='') {
		//TODO - Insert your code here
		parent::__construct();
		$this->id = $id;
		$inp = Request::prop( $this->id."_tracker" );
		if( ISSET( $inp ) ){
			$this->currentNum = $inp;
		}
		$this->dat = $this->HtmlElement('div');
		$this->dat->addAttribute('id', $id);
		if( !empty($class) ){
			$this->dat->addAttribute('class', $class);
		}
		$this->addJSInit( "function MovePageNavigator(num){
			$('#".$this->id."_tracker').val(num);
			$('#".$this->id."_form').submit(); 
		};" );
	}

	public function finalize(){
		$this->addControls();
		$this->output = new stdClass();
		$this->output->content = (string)$this->cleanseXMLString($this->dat->asXML());
	}

	/**
	 * last minute addition of controls
	 */
	protected function addControls( ){
		$controls = $this->HtmlElement('form','',Array('id'=>$this->id."_form",'method'=>'POST'));
		$controls->addAttribute('class', 'nav_controls');
		$controls= $controls->addChild( $this->GetLeftNav() );	
		$controls=$this->GenerateLinks( $controls );
		$controls= $controls->addChild( $this->GetRightNav() );
		$tracker = $this->HtmlElement('hidden','',Array('name'=>$this->id."_tracker",'id'=>$this->id."_tracker",'Value'=>$this->currentNum ));	
		$controls = $controls->addChild( $tracker );	
		$this->dat = $this->dat->addChild( $controls );
	}
	/**
	 * 
	 * Get the left hand link
	 */
	protected function GetLeftNav(){
		$controls = $this->HtmlElement('a',' ');
		$controls->addAttribute('class', 'PageNav');
		$btn = $this->HtmlElement('span',' ',Array( 'class' => '' ) );
		if($this->currentNum>0){
			$icon = $this->HtmlElement('a',' ',Array( 'class' => 'fa fa-chevron-circle-left fa-2x ','href'=>'javascript:MovePageNavigator('.($this->currentNum-1).');' ) );
		}
		else {
			$icon = $this->HtmlElement('a',' ',Array( 'class' => 'fa fa-chevron-circle-left fa-2x disabled','disabled'=>'disabled') );
		}
		$btn = $btn->addChild( $icon );
		
		$controls = $controls->addChild($btn);
		return $controls;
	}
	
	protected function GenerateLinks( $controls ){
		$num_of_links = round($this->collectionTotal / $this->limit,0,PHP_ROUND_HALF_UP);
		for($i = 0;$i<$num_of_links;$i++){
			$col = $i+1;
			$link = $this->HtmlElement('a',"$col" );
			$btn = $this->HtmlElement('span','',Array( 'class' => 'PNSelector' ) );
//			$icon = $this->HtmlElement('i',' ',Array( 'class' => 'fa fa-chevron-circle-left fa-2x ' ) );
			$btn = $btn->addChild( $link );
			//$link = $link->addChild($btn);
			$controls = $controls->addChild( $btn );
		}
		return $controls;
	}
	/**
	 * 
	 * Get the right hand link
	 */
	protected function GetRightNav(){
		$controls = $this->HtmlElement('a',' ');
		$controls->addAttribute('class', 'PageNav');
		$btn = $this->HtmlElement('span',' ',Array( 'class' => '' ) );
		$icon = $this->HtmlElement('a',' ',Array( 'class' => 'fa fa-chevron-circle-right fa-2x ','href'=>'javascript:MovePageNavigator('.($this->currentNum+1).');' ) );
		$btn = $btn->addChild( $icon );
		
		$controls = $controls->addChild($btn);
		return $controls;
	}
}


?>